﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Configuration;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
using System.Threading;

namespace Hybrid
{
    public partial class HostGame : Form
    {
        Main mainFrm;
        private TcpListener tcpListener;
        private Thread listenThread;

        public HostGame()
        {
            InitializeComponent();
            this.tcpListener = new TcpListener(IPAddress.Any, 8001);
            this.listenThread = new Thread(new ThreadStart(ListenForClients));
            this.listenThread.Start();
            label1.Text = "192.168.0.3";
        }
              
        private void ListenForClients()
        {
            this.tcpListener.Start();

            while (true)
            {
                //blocks until a client has connected to the server
                TcpClient client = this.tcpListener.AcceptTcpClient();

                //create a thread to handle communication
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
                clientThread.Start(client);
            }
        }

        private void HandleClientComm(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                AppendTextBox(encoder.GetString(message, 0, bytesRead));
            }

            tcpClient.Close();
        }

        private delegate void AppendTextBoxDelegate(string value);

        private void AppendTextBox(string value)
        {
            if (this.label2.InvokeRequired)
            {
                // This is a worker thread so delegate the task.
                this.label2.Invoke(new AppendTextBoxDelegate(this.AppendTextBox), value);
            }
            else
            {
                // This is the UI thread so perform the task.
                this.label2.Text += Environment.NewLine + value;
            }
        }


    }
}
